'use strict';

const Controller = require('egg').Controller;

class OrderController extends Controller {

  async getOrderLatestDetail() {
    const { ctx } = this;

    const listQuery = {
      state: 'PAY',
      user_id: ctx.getUserId(),
    };
    
    
    const [ option ] = ctx.getPageListOption();
    option.limit = 1;
    option.sort = '-create_at';

    const [ list ] = await ctx.service.order.getOrderList(listQuery, option);

    if (!list || list.length === 0) {
      ctx.body = {
        success: false,
      };
      return ;
    }

    const [ detail ] = list;

    const [ orderDetail, expertDetail ] = await ctx.service.order.getOrderDetail({
      orderId: detail._id,
      userId: ctx.getUserId()
    });

    const [ os ] = await ctx.service.order.getSubscriptionOrderDetailByQuery({
      order_id: detail._id,
      user_id: ctx.getUserId()
    });

    const fromUserId = expertDetail.user_id;
    const chatRoomId = os.chat_room_id;

    let messageData = null;

    if (orderDetail.order_biz_type === 'picture') {
      const [ latestMessaeg ] = await ctx.service.chat.getLatestMessage({
        chatRoomId,
        fromUserId,
      });
      messageData = latestMessaeg;
    }
    
    ctx.body = {
      success: true,
      orderDetail,
      expertDetail,
      content: os,
      latestMessaeg: messageData,
    };
  }

  async getPayment() {
    const { ctx } = this;
    const body = ctx.request.body;
    const state = body.state;

    if (state === 'NOPAY') {
      const [ payment ] = await ctx.service.payment.postPaymentPay({
        orderId: body.orderId,
        userId: ctx.getUserId(),
      });

  
      ctx.body = {
        success: true,
        payment,
      };
      return;
    }

    if (state === 'PAY') {
      const [ payment ] = await ctx.service.payment.postPaymentDone({
        orderId: body.orderId,
        userId: ctx.getUserId(),
      });

      ctx.body = {
        success: true,
        payment,
      };
      return;
    }
  }

  async getOrderList() {
    const { ctx } = this;

    const state = ctx.getQuery('state');
    const [ option ] = ctx.getPageListOption();
    
    option.sort = '-create_at';

    const listQuery = {
      state,
      user_id: ctx.getUserId(),
    };
    
    const totalCount = await ctx.service.order.getCountByQuery(listQuery);
    const [ list ] = await ctx.service.order.getOrderList(listQuery, option);

    ctx.apiPageList(list, totalCount);
  }

  async getOrderDetail() {
    const { ctx } = this;

    const id = ctx.getQuery('id');

    const [ orderDetail, expertDetail, os ] = await ctx.service.order.getOrderDetail({
      orderId: id,
      userId: ctx.getUserId(),
    });

    ctx.body = {
      success: true,
      orderDetail,
      expertDetail,
      os,
    };
  }

  async getSubscriptionOrderDetail() {
    const { ctx } = this;
    const id = ctx.getQuery('id');
    
    const [ detail, expertDetail, userDetail, orderDetail ] = await ctx.service.order.getSubscriptionOrderDetail({
      subscriptionOrderId: id,
      userId: ctx.getUserId(),
    });

    ctx.body = {
      success: true,
      subscriptionDetail: detail,
      expertDetail,
      userDetail,
      orderDetail,
    };
  }
  
  async getSubscriptionOrderList() {
    const { ctx } = this;
    
    const type = ctx.getQuery('type');

    const [ option ] = ctx.getPageListOption();
    
    option.sort = '-create_at';

    const listQuery = {
      type,
      user_id: ctx.getUserId(),
    };

    const totalCount = await ctx.service.order.getSubscriptionCountByQuery(listQuery);
    const [ list ] = await ctx.service.order.getSubscriptionOrderList(listQuery, option);
    
    ctx.apiPageList(list, totalCount);
  }
  
  async createSubscriptionOrder() {
    const { ctx } = this;
    const body = ctx.request.body;

    const [ code, o ] = await ctx.service.order.newOrder({
      ...body,
      userId: ctx.getUserId(),
    });

    ctx.body = {
      success: true,
      code,
      orderId: o._id,
    };
  }
}

module.exports = OrderController;